package com.dunqi.gpm.shunqing.home.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.dunqi.gpm.shunqing.R;
import com.dunqi.gpm.shunqing.common.api.ApiFactory;
import com.dunqi.gpm.shunqing.common.api.RxSchedulers;
import com.dunqi.gpm.shunqing.common.base.BaseFragment;
import com.dunqi.gpm.shunqing.common.util.SwipeUtil;
import com.dunqi.gpm.shunqing.common.view.RecyclerItemDecoration;
import com.dunqi.gpm.shunqing.home.activity.ProblemConsortDetailActivity;
import com.dunqi.gpm.shunqing.home.activity.QuestionHelpListActivity;
import com.dunqi.gpm.shunqing.home.activity.SolveProblemActivity;
import com.dunqi.gpm.shunqing.home.adapter.ProblemConsortAdapter;
import com.dunqi.gpm.shunqing.home.bean.Page;
import com.dunqi.gpm.shunqing.home.bean.Problem;
import com.dunqi.gpm.shunqing.home.constance.Constance;
import com.dunqi.gpm.shunqing.home.eventbus.SolveProblemEvent;
import com.dunqi.gpm.shunqing.home.widget.EmptyView;
import com.dunqi.gpm.shunqing.my.util.UserUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.HashMap;

import butterknife.BindView;
import io.reactivex.functions.Consumer;

/**
 * Created by 空 on 2017/11/30 0030.
 */

public class ProblemConsortFragment2 extends BaseFragment implements SwipeRefreshLayout.OnRefreshListener, BaseQuickAdapter.RequestLoadMoreListener, BaseQuickAdapter.OnItemClickListener {
    @BindView(R.id.recycler)
    RecyclerView recycler;
    @BindView(R.id.swipeRefresh)
    SwipeRefreshLayout swipeRefresh;
    private long projectId;
    private String projectName;
    ArrayList<Problem> data = new ArrayList<>();
    private ProblemConsortAdapter adapter;

    HashMap<String, String> hashMap = new HashMap<>();

    @Override
    protected void refreshData(Bundle savedInstanceState) {

        if (projectId != -1) {
            hashMap.put("project_id", String.valueOf(projectId));
            hashMap.put("project_name", projectName);
        }

        net();
    }

    private void net() {
        hashMap.put("page", String.valueOf(page));
        int levelType = UserUtil.getLevelType();

        if (UserUtil.userIsLeader()) {
            hashMap.put("auditStatus", "2,4,5");
        }

        ApiFactory.getInstance()
                .queryProjectProblemList(hashMap)
                .compose(RxSchedulers.<Page<Problem>>io_main())
                .subscribe(new Consumer<Page<Problem>>() {
                    @Override
                    public void accept(Page<Problem> problemPage) throws Exception {
                        SwipeUtil.loadCompleted(swipeRefresh);
                        ArrayList<Problem> list = problemPage.getList();

                        if (page == 1) {
                            adapter.replaceData(list);
                        } else {
                            adapter.addData(list);
                        }

                        if (problemPage.isHasNextPage()) {
                            adapter.loadMoreComplete();
                        } else {
                            adapter.loadMoreEnd(true);
                        }


                        page++;

                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        SwipeUtil.loadCompleted(swipeRefresh);
                        adapter.loadMoreFail();
                    }
                });
    }

    @Override
    protected void initUiAndListener() {

        SwipeUtil.init(swipeRefresh);
        recycler.setLayoutManager(new LinearLayoutManager(context));
        adapter = new ProblemConsortAdapter(data);
        recycler.setAdapter(adapter);
        adapter.setEmptyView(new EmptyView(context));

        recycler.addItemDecoration(new RecyclerItemDecoration(1, RecyclerItemDecoration.VERTICAL));
        swipeRefresh.setOnRefreshListener(this);
        adapter.setOnLoadMoreListener(this, recycler);
        adapter.setOnItemClickListener(this);

    }

    @Override
    public void onRefresh() {
        page = 1;
        net();
    }

    @Override
    public void onLoadMoreRequested() {
        net();
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        int type = HomeFragment.homeMenuId;
        int levelType = UserUtil.getLevelType();

        Problem item = data.get(position);

        long projectProblemId = item.getProjectProblemId();
        int itemProblemStatus = item.getProblemStatus();
        QuestionHelpListActivity.start(context,projectProblemId,itemProblemStatus);

        if (true) {
            return;
        }

        if (type == 11) {
            ProblemConsortDetailActivity.start(context, item);
        }

        if (type == 18 && UserUtil.userFromNdrcOrg()) {
            SolveProblemActivity.start(getActivity(), item.getProjectProblemId(), item.getProblemStatus());
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == 607) {
            page = 1;
            net();
        }
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_blank;
    }


    @Subscribe
    public void refresh(SolveProblemEvent event){
        if (swipeRefresh != null) {
            swipeRefresh.setRefreshing(true);
        }
        onRefresh();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
        Bundle arguments = getArguments();
        projectId = arguments.getLong(Constance.PROJECTID, -1);
        projectName = arguments.getString(Constance.NAME);
    }

    public static ProblemConsortFragment2 newInstance(long projectId, String projectName) {
        ProblemConsortFragment2 fragment = new ProblemConsortFragment2();
        Bundle bundle = new Bundle();
        bundle.putLong(Constance.PROJECTID, projectId);
        bundle.putString(Constance.NAME, projectName);
        fragment.setArguments(bundle);
        return fragment;
    }
}
